import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class DropdownSliverHeader extends StatefulWidget {
  const DropdownSliverHeader({super.key});

  @override
  DropdownSliverHeaderState createState() => DropdownSliverHeaderState();
}

class DropdownSliverHeaderState extends State<DropdownSliverHeader> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('模板'),
        ),
        body: CustomScrollView(
          slivers: [
            SliverPersistentHeader(
              floating: true,
              delegate: ShowOnScreenSPHD(height: 60),
            ),
            SliverPersistentHeader(
              pinned: true,
              delegate: FixedPersistentHeaderDelegate(height: 60),
            ),
            SliverList.list(
              children: List.generate(20, (index) {
                return Container(
                  height: 50,
                  color: Colors.blue.withOpacity(0.1 * (index % 10)),
                );
              }).toList(),
            ),
          ],
        ));
  }
}

class FixedPersistentHeaderDelegate extends SliverPersistentHeaderDelegate {
  final double height;

  FixedPersistentHeaderDelegate({required this.height});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      height: height,
      alignment: Alignment.center,
      color: Colors.red,
      child: Text(
        'FixedPersistentHeader',
        style: TextStyle(
            color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold),
      ),
    );
  }

  @override
  double get maxExtent => height;

  @override
  double get minExtent => height;

  @override
  bool shouldRebuild(covariant FixedPersistentHeaderDelegate oldDelegate) {
    return oldDelegate.height != height;
  }
}

class ShowOnScreenSPHD extends SliverPersistentHeaderDelegate {
  final double height;

  ShowOnScreenSPHD({required this.height});

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container(
      height: height,
      alignment: Alignment.center,
      color: Colors.orange,
      child: Text(
        'ShowOnScreenSPHD',
        style: TextStyle(
          color: Colors.white,
          fontSize: 16,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  @override
  double get maxExtent => height;

  @override
  double get minExtent => height;

  @override
  bool shouldRebuild(covariant ShowOnScreenSPHD oldDelegate) {
    return oldDelegate.height != height;
  }

  @override
  PersistentHeaderShowOnScreenConfiguration get showOnScreenConfiguration {
    return const PersistentHeaderShowOnScreenConfiguration(
      minShowOnScreenExtent: double.infinity,
    );
  }
}
